<?php

namespace app\resources\tasks;

use help\SystemHelp;
use think\facade\Cache;
use app\resources\model\Nodes;


// app('swoole.server')->task([ NodeTask::class, [
//         'type'=>1,
//         'data'=>'asdsada'
// ]]);

// ws://127.0.0.1:5375/ws?action=web&token=eyJ0eXAiOiJKV1Qi

// 推送 websocket 消息

class NodeTask
{

    protected $params = [];

    public function __construct($params = [])
    {
        $this->params = $params;
        $this->start();
    }

    // 执行
    public function start()
    {
        if( $this->params['type'] == 'reset' ){
            $this->reset();
        }
    }

    // 重置
    public function reset()
    {
        $node = Nodes::find($this->params['data']);
        if (!$node) {
            return;
        }
        $server = env('APP_HOST');
        $res = SystemHelp::sshCmd($node->host, $node->username, $node->password, "curl -sSL $server/system/node/install?reset=1 | sh");
        if ($res) {
            throw new \think\Exception('部署失败，请检查节点配置是否正确！');
        }
    }
}
